//
//  HostoryTodayVC.m
//  renyikan
//
//  Created by Steven on 2019/7/16.
//  Copyright © 2019 Steven. All rights reserved.
//

#import "HostoryTodayVC.h"
#import "HistoryCell.h"


@interface HostoryTodayVC () <UITableViewDelegate, UITableViewDataSource>
@property(nonatomic, weak)IBOutlet UITableView *tableView;

@property(nonatomic, strong)NSMutableArray *dataArray;

@end

@implementation HostoryTodayVC

- (NSMutableArray *)dataArray{
    if (_dataArray == nil) {
        _dataArray = [NSMutableArray array];
    }
    return _dataArray;
}



- (void)viewDidLoad {
    [super viewDidLoad];
    self.tableView.estimatedRowHeight = 60;
    self.tableView.rowHeight = UITableViewAutomaticDimension;
    
    self.tableView.tableFooterView = [UIView new];
    
    
    [self getDatas];
    
}
- (void)getDatas{
    
    
     // 获取代表公历的NSCalendar对象
    NSCalendar *gregorian = [[NSCalendar alloc]
                             initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
    // 获取当前日期
    NSDate* dt = [NSDate date];
    // 定义一个时间字段的旗标，指定将会获取指定年、月、日、时、分、秒的信息
    unsigned unitFlags = NSCalendarUnitYear |
    NSCalendarUnitMonth |  NSCalendarUnitDay |
    NSCalendarUnitHour |  NSCalendarUnitMinute |
    NSCalendarUnitSecond | NSCalendarUnitWeekday;
    // 获取不同时间字段的信息
    NSDateComponents* comp = [gregorian components: unitFlags
                                          fromDate:dt];
    // 获取各时间字段的数值
    NSLog(@"现在是%ld年" , comp.year);
    NSLog(@"现在是%ld月 " , comp.month);
    NSLog(@"现在是%ld日" , comp.day);
    NSLog(@"现在是%ld时" , comp.hour);
    NSLog(@"现在是%ld分" , comp.minute);
    NSLog(@"现在是%ld秒" , comp.second);
    NSLog(@"现在是星期%ld" , comp.weekday);
    // 再次创建一个NSDateComponents对象
//    NSDateComponents* comp2 = [[NSDateComponents alloc]
//                               init];
//    // 设置各时间字段的数值
//    comp2.year = 2013;
//    comp2.month = 4;
//    comp2.day = 5;
//    comp2.hour = 18;
//    comp2.minute = 34;
//    // 通过NSDateComponents所包含的时间字段的数值来恢复NSDate对象
//    NSDate *date = [gregorian dateFromComponents:comp2];
//    NSLog(@"获取的日期为：%@" , date);
    
    
    
    __weak typeof(self) weakSelf = self;
    
    [JFNetworkingManager requestWithURLString:[NSString stringWithFormat:@"http://api.juheapi.com/japi/toh?v=1.0&month=%ld&day=%ld&key=dcc5b53c9d6148280f9b0b3e63439d11", (long)comp.month, (long)comp.day] parameters:nil type: HttpRequestTypePost view: self.view success:^(id responseObject) {
        
        weakSelf.dataArray = responseObject[@"result"];
        [weakSelf.tableView reloadData];
        
    } failure:^(NSError *error) {
        
    }];
    
    
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    
    NSDictionary *data = self.dataArray[indexPath.row];
    HistoryCell *cell = [tableView dequeueReusableCellWithIdentifier:@"HistoryCell" forIndexPath:indexPath];

    cell.titleLabel.text = data[@"title"];
    cell.descLabel.text = data[@"des"];
    cell.hanTimeLabel.text = data[@"lunar"];
    cell.timeLabel.text = [NSString stringWithFormat:@"%@年%@月%@日", data[@"year"], data[@"month"], data[@"day"]];
    return cell;
}


@end
